草庐IT

php - Golang 常量结构键

全部标签

go - 如何在func中修改全局结构值

当我使用它在结构上迭代时,内存地址是不同的。所以我不能修改它的值没有人typeSiteUrlstruct{namestringurlstringisUpbool}funcdebug(s*SiteUrl){s.isUp=false}funcmain(){sites:=[]SiteUrl{{"testsite","http://127.0.0.1:8000",true},}for{for_,site:=rangesites{fmt.Println(&site.isUp,site.isUp)debug(&site)}}}它的值没有修改 最佳答案

golang 只从 json 获取一个统计信息

我试图从json中的webapi请求中获取一个统计信息。这就是所谓的https://api.coinmarketcap.com/v1/ticker/ethereum/我使用这个github代码示例//获取关于硬币的信息coinInfo,err:=coinApi.GetCoinData("ethereum")iferr!=nil{log.Println(err)}else{fmt.Println(coinInfo)}我在日志中的结果是{ethereumEthereumETH2830.480.1002873.23573e+098.0977392218e+109.7506734e+079.7

go - 我如何在 golang 中使用本地包?

这个问题在这里已经有了答案:Error"can'tloadpackage:packagemy_prog:foundpackagesmy_progandmain"(3个答案)关闭4年前。这里有两个.go文件。├──lib.go└──main.golib.go有一个包libtest。$catlib.gopackagelibtestimport("fmt")funcTestLibFunc(){fmt.Println("Thisistestlibraryfunction")}main.go有一个包main。$catmain.gopackagemainimport("libtest")funcm

go - 如何在 Golang 中构建 URL/查询

背景-我需要根据来自表单的用户输入构建一个URL/查询,该表单将用于进行API调用。问题-构建URL时,参数未正确转义。例如,查询“badsanta”以空格结尾,而不是“+”。当前输出-e.g.https://api.example.org/3/search/movie?query=badsanta&api_key=#######预期输出-e.g.https://api.example.org/3/search/movie?query=bad+santa&api_key=#######代码示例-根网址-varSearchUrl="https://www.example.org/3/se

go - 为什么我在其他函数中设置值的结构中的字段始终为零?

这个问题在这里已经有了答案:Usingasetterforastructtypedoesnotworkasanticipated(2个回答)PropertyinGolangstructnotgettingmodified(2个回答)Structvariablenotbeingupdated(1个回答)2年前关闭。我在其他方法中为结构设置字段值。我断点,session有一个值,但是当getSession时返回,session为零。我感觉很失落。像代码一样,newMongoUtil创建MongoUtil,以及mu.getSession给MongoUtil.session如果是nil,则为一

go - &http.Client 在 Golang 中,需要解释

我找到了下面的代码客户端:=&http.客户端&是什么意思?clientvar接收什么样的值 最佳答案 &是“指针”运算符,类似于c.client变量包含指向http.Client值的指针。 关于go-&http.Client在Golang中,需要解释,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/52923549/

go - 如何使用 Golang 获取发布的二进制数据的文件名?

我使用PostmanRESTAPI客户端将一个csv文件作为二进制文件发布。我需要获取上传文件的文件名。这是一个将csv文件作为二进制数据发布并将二进制数据存储为csv文件的简单示例。packagemainimport(//"fmt""net/http""os""io""log""github.com/gorilla/mux")funcuploadData(whttp.ResponseWriter,req*http.Request){file,err:=os.Create("hello.csv")_,err=io.Copy(file,req.Body)_=err}funcmain(){

if-statement - golang if 初始化语句作用域为内部 if block 。为什么?

这个问题在这里已经有了答案:Golangmixedassignmentanddeclaration(4个答案)关闭4年前。我在我的代码中发现了一个错误funcreceive()(errerror){ifv,err:=produce();err==nil{fmt.Println("value:",v)}return}此函数永远不会返回错误,但我绝对确定它应该返回。经过一些测试,我了解到err在if语句中被重新声明。不仅如此-所有变量总是在if语句内的短变量赋值中重新声明,尽管它们之前已经声明过。这是工作代码funcreceive()(errerror){v,err:=produce()i

go - 未定义的func,即使在golang中导入

我在GitHub下有一个项目,并且在我的机器上本地工作。下面是项目的结构.weather:-weather-service.go-darksky-provider.go.grpc-weather:-weather.protoserver.goserver.go文件的完成如下:import(pb"github.com/scayetanot/weather_service_go/grpc_weather""net""log""golang.org/x/net/context""google.golang.org/grpc""google.golang.org/grpc/reflection

go - 如何在golang中将 slice 附加到字节数组

我尝试将动态slice传递给这个jsonstrslicevarcatlist[]stringvarjsonStr=[]byte(`{"categoryList":`+catlist+`}`)但是我得到了这个错误invalidoperation:"{\"categoryList\":"+catlist(mismatchedtypesstringand[]string)如何修复这个错误 最佳答案 您正在连接string和[]string!!这是不可能的!你必须使用json包:Packagejson